package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {


    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Autowired(required = false)
    private DishMapper dishMapper;

    @Autowired(required = false)
    private SetmealMapper setmealMapper;


    /**
     * 添加种类
     *
     * @param category@return
     */
    @Override
    public boolean save(Category category) {
        //1.完善数据
        // 创建时间与修改时间
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        //2.判断类别名是否重复
        Category byName = categoryMapper.findByName(category.getName());
        if (byName != null) {
            throw new BusinessException("该类名已存在,请重新输入");
        }
        //3.调用数据访问层添加数据
        return categoryMapper.save(category) > 0;
    }

    /**
     * 查询(分类分页查询)
     *
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {
        //1.pageHelper设置当前页和每页的大小
        PageHelper.startPage(page,pageSize);

        //2.调用数据访问层查询列表数据
        List<Category> all = categoryMapper.findAll();

        //3.封装PageInfo对象
        PageInfo<Category> categoryPageInfo = new PageInfo<>(all);

        Page<Category> objectPage = new Page<>();
        objectPage.setRecords(categoryPageInfo.getList());
        objectPage .setTotal(categoryPageInfo.getTotal());
        return objectPage;
    }






    /**
     * 删除指定类别
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteById(Long id) {

        //1.获取类别菜品数量,如果>0,抛出异常"该类别有菜品无法删除"
        Integer disCount = dishMapper.countByCid(id);
        if (disCount > 0) {
            throw new BusinessException("该类有菜品无法删除");
        }

        //2.获取套餐信息数量,如果>0,抛出异常"该类别有套餐信息无法删除"
        Integer setCount = setmealMapper.countByCid(id);
        if (setCount > 0) {
            throw new BusinessException("该类别有套餐信息无法删除");
        }

        //3.调用数据访问层删除
        return categoryMapper.deleteById(id) > 0;
    }


    /**
     * 修改类别
     *
     * @param category
     * @return
     */
    @Override
    public boolean update(Category category) {

        //1.判断这个类别名称是否存在
        Category mapper = categoryMapper.findByName2(category);
        if (mapper != null) {
            throw new BusinessException("类别名已存在,请重新修改");
        }
        //2.完善信息(修改时间)
        category.setUpdateTime(LocalDateTime.now());

        //3.调用数据访问层操作修改

        return categoryMapper.update(category) > 0;
    }



    //查询所有
    public List<Category> findAll(){
        return categoryMapper.findAll();
    }


    /**
     * 根据type查询分类列表数据
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> findAllByType(Integer type) {

        return categoryMapper.findAllByType(type);
    }
}